package com.store.app.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.store.app.BaseActivity;
import com.store.app.ExitApplication;
import com.store.app.MainActivity;
import com.store.app.R;
import com.store.app.adapter.OneYuanBuyAdapter;
import com.store.app.bean.AllBean;
import com.store.app.bean.OneYuanBuyBean;
import com.store.app.bean.OneYuanBuyUpdateBean;
import com.store.app.net.NetRequest;
import com.store.app.net.ifc.IOnHttpListener;
import com.store.app.utils.UserTokenUtils;
import com.store.app.utils.Utils;
import com.store.app.widget.AutoListView;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import de.greenrobot.event.EventBus;

/**
 * Created by Administrator on 2016/5/11.
 * 一元抽奖页面
 */
public class OneYuanBuyActivity extends BaseActivity implements IOnHttpListener ,View.OnClickListener{
    private TextView tv_title;
    private AutoListView listview;
    private NetRequest netRequest;


    private ImageView img_fail_nonet;
    private ImageView img_fail_notext;
    private TextView tv_fail;
    private List<OneYuanBuyBean> list = new ArrayList<>();
    private Map<String, String> map = new HashMap();
    private LinearLayout lin_loadNull;//加载为空
    private LinearLayout lin_loadfail;  //无网络页面
    private TextView tvReload;
    private int pageNum = 1;

    private RelativeLayout loadfail;//加载状态背景
    private OneYuanBuyAdapter adapter;
    private LinearLayout ll_yyy;
    private LinearLayout ll_ggl;
    private LinearLayout ll_dsk;
    private RelativeLayout rl_dialog_view;

    public void showLoadResult(int state) {
        //无网络情况
        if (state == 1) {
            loadfail.setVisibility(View.VISIBLE);
            lin_loadfail.setVisibility(View.VISIBLE);
            lin_loadNull.setVisibility(View.GONE);
        }//无数据情况
        else if (state == 2) {
            loadfail.setVisibility(View.VISIBLE);
            lin_loadfail.setVisibility(View.GONE);
            Log.v("zyl", "加载内容为空");
            lin_loadNull.setVisibility(View.VISIBLE);
        }//都不显示
        else {
            loadfail.setVisibility(View.GONE);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ExitApplication.getInstance().addActivity(this);
        setContentView(R.layout.activity_oneyuanbuy);
        initView();
        initData();
        initListeners();
        EventBus.getDefault().register(this);

    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    private void initData() {
        UserTokenUtils.setUerToken(this);
        netRequest = new NetRequest(this);
        adapter = new OneYuanBuyAdapter(list, map, this);
        listview.setAdapter(adapter);
        listview.setDivider(null);


    }

    private void initListeners() {
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position >= 1 && position <= list.size()) {
                    Intent intent = new Intent(OneYuanBuyActivity.this, OneYuan_DetailsActivity.class);
                    intent.putExtra("id", list.get(position - 1).getActivity_id());
                    intent.putExtra("luck_no", list.get(position - 1).getLuck_code());
                    intent.putExtra("title", list.get(position - 1).getTitle());
                    intent.putExtra("shop_name", list.get(position - 1).getAward_name());
                    intent.putExtra("end_time", list.get(position - 1).getEnd_date());
                    intent.putExtra("shop_price", list.get(position - 1).getAward_value());
                    intent.putExtra("limit", list.get(position - 1).getPerson_num());
                    intent.putExtra("pic", map.get(list.get(position - 1).getAward_pic_path1()));
                    intent.putExtra("pic_id", list.get(position - 1).getAward_pic_path1());
                    intent.putExtra("status", list.get(position - 1).getStatus());
                    intent.putExtra("activity_type",list.get(position-1).getActivity_type());
                    startActivity(intent);
                }
            }
        });
        listview.setOnRefreshListener(new AutoListView.OnRefreshListener() {

            @Override
            public void onRefresh() {
                pageNum = 1;
                if(!TextUtils.isEmpty( MainActivity.member_id)){
                    netRequest.requestOneYuanQuery(2, MainActivity.member_id, pageNum);
                }

            }
        });

        listview.setOnLoadListener(new AutoListView.OnLoadListener() {

            @Override
            public void onLoad() {
                if(!TextUtils.isEmpty( MainActivity.member_id)){
                    netRequest.requestOneYuanQuery(1, MainActivity.member_id, pageNum);
                }
            }
        });
        listview.onLoad();
        findViewById(R.id.oneyuanbuy_btn1).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                rl_dialog_view.setVisibility(View.VISIBLE);
//                Intent intent = new Intent(OneYuanBuyActivity.this, OneYuanBuy_CreatActivity.class);
//                startActivity(intent);
            }
        });
        findViewById(R.id.oneyuanbuy_btn2).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(OneYuanBuyActivity.this, OneYuanBuy_CheckActivity.class);
                startActivity(intent);
            }
        });
        findViewById(R.id.public_ll_return).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        tvReload = (TextView) findViewById(R.id.tv_reload);
        tvReload.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Utils.isFastClick()) {
                    return;
                }
                if (listview != null) {
                    list.clear();
                    adapter.notifyDataSetChanged();
                    pageNum = 1;
                    listview.onLoad();
                }

            }
        });
    }

    private void initView() {
        loadfail = (RelativeLayout) findViewById(R.id.loadfail);
        tv_title = (TextView) findViewById(R.id.tvTitle);
        tv_title.setText("一元抽奖");
        listview = (AutoListView) findViewById(R.id.onebuy_listview);
        lin_loadfail = (LinearLayout) findViewById(R.id.ll_load_fail);//无网络
        lin_loadNull = (LinearLayout) findViewById(R.id.ll_load_null);//无数据
        loadfail.setVisibility(View.GONE);
        img_fail_nonet = (ImageView) findViewById(R.id.loadfail_nonet);
        img_fail_notext = (ImageView) findViewById(R.id.loadfail_notext);
        tv_fail = (TextView) findViewById(R.id.loadfail_tv);
        ll_yyy = (LinearLayout) findViewById(R.id.ll_yyy);
        ll_yyy.setOnClickListener(this);
        ll_ggl = (LinearLayout) findViewById(R.id.ll_ggl);
        ll_ggl.setOnClickListener(this);
        ll_dsk = (LinearLayout) findViewById(R.id.ll_dsk);
        ll_dsk.setOnClickListener(this);
        rl_dialog_view = (RelativeLayout) findViewById(R.id.rl_dialog_view);
        rl_dialog_view.setOnClickListener(this);
    }

    @Override
    public void onAfinalFail() {
        listview.onLoadComplete();
        listview.onRefreshComplete();
//        if (list != null && list.size() == 0) {
////            lin_loadfail.setVisibility(View.VISIBLE);
////            lin_loadfail.setOnClickListener(new View.OnClickListener() {
////                @Override
////                public void onClick(View v) {
////                    lin_loadfail.setVisibility(View.GONE);
////                    listview.onLoad();
////                }
////            });
        showLoadResult(1);
//        }
    }

    @Override
    public void onExecuteSuccess(int requestId, String result, String msg) {
        if (requestId == 1) {
            Log.v("mylog", "result:" + result);
            try {
                Type type = new TypeToken<AllBean<OneYuanBuyBean>>() {
                }.getType();
                Gson gson = new Gson();
                AllBean<OneYuanBuyBean> allBean = gson.fromJson(result, type);
                listview.onLoadComplete();
                List<OneYuanBuyBean> list_one = allBean.getList();
                Map<String, String> map_one = allBean.getDoc_url();
                if (list_one == null) {
                    return;
                }
                listview.setResultSize(list_one.size());
                list.addAll(list_one);
                map.putAll(map_one);
                adapter.notifyDataSetChanged();
                pageNum += 1;

                if (list.size() == 0) {
//                    lin_loadfail.setVisibility(View.VISIBLE);
//                    img_fail_nonet.setVisibility(View.GONE);
//                    img_fail_notext.setVisibility(View.VISIBLE);
//                    tv_fail.setText("暂无内容");
                    showLoadResult(2);
                } else {
                    showLoadResult(3);
                }

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        if (requestId == 2) {
            Log.v("mylog", "ff result:" + result);
            try {
                Type type = new TypeToken<AllBean<OneYuanBuyBean>>() {
                }.getType();
                Gson gson = new Gson();
                AllBean<OneYuanBuyBean> allBean = gson.fromJson(result, type);
                List<OneYuanBuyBean> list_one = allBean.getList();
                Map<String, String> map_one = allBean.getDoc_url();
                listview.onRefreshComplete();
                listview.setResultSize(list_one.size());
                list.clear();
                list.addAll(list_one);
                map.putAll(map_one);
                adapter.notifyDataSetChanged();
                pageNum += 1;
                if (list.size() == 0) {
                    showLoadResult(2);
                } else {
                    showLoadResult(3);
                }

                listview.setSelection(0);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }


    }

    @Override
    public void onExecuteFail(int requestId, String msg) {
        if (requestId == 1) {
            listview.setResultSize(0);
            listview.onLoadComplete();
        } else if (requestId == 2) {
            listview.onRefreshComplete();
        }

    }

    public void onEvent(OneYuanBuyUpdateBean event) {
        if (event.type == 1) {
            pageNum = 1;
//
            showLoadResult(3);
            netRequest.requestOneYuanQuery(2, MainActivity.member_id, pageNum);
        }
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.rl_dialog_view:
                rl_dialog_view.setVisibility(View.GONE);
                break;
            //摇一摇
            case R.id.ll_yyy:
                rl_dialog_view.setVisibility(View.GONE);
                Intent intentToYYY = new Intent(OneYuanBuyActivity.this,CreateYYYActivity.class);
                startActivity(intentToYYY);
                break;
            //刮刮乐
            case R.id.ll_ggl:
                rl_dialog_view.setVisibility(View.GONE);
                Intent intentToGGL = new Intent(OneYuanBuyActivity.this,CreateGGLActivity.class);
                startActivity(intentToGGL);
                break;
            //定时开
            case R.id.ll_dsk:
                rl_dialog_view.setVisibility(View.GONE);
                Intent intent = new Intent(OneYuanBuyActivity.this, OneYuanBuy_CreatActivity.class);
                startActivity(intent);
                break;
        }
    }
}
